using BunnyEater.Common.ResponseOutput;
using BunnyEater.Domain.Base;
using BunnyEater.Service.TraceableService.FirmService;
using BunnyEater.Service.TraceableService.FirmService.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using BunnyEater.Common.Enum;
using System.ComponentModel;
using System.Reflection;
using Microsoft.AspNetCore.Authorization;

namespace BunnyEater.WebAPI.Controllers.Traceable
{
    /// <summary>
    /// 企业管理控制器，提供企业相关的增删改查接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "TRACE")]
    [ApiController]
    public class FirmController : ControllerBase
    {
        private readonly IFirmService _firmService;

        public FirmController(IFirmService firmService)
        {
            _firmService = firmService;
        }

        /// <summary>
        /// 分页获取企业列表
        /// </summary>
        /// <param name="query">分页查询参数</param>
        /// <returns>企业分页数据</returns>
        [HttpGet]
        public async Task<ResponseReturnsEntity<Page<FirmListDto>>> GetPagedFirms([FromQuery] FirmPagedQueryDto query)
        {
            return await _firmService.GetPagedFirmsAsync(query);
        }

        /// <summary>
        /// 根据Id获取企业信息
        /// </summary>
        /// <param name="id">企业Id</param>
        /// <returns>企业信息</returns>
        [HttpGet]
        public async Task<ResponseReturnsEntity<FirmListDto>> GetFirmById(string id)
        {
            return await _firmService.GetFirmById(id);
        }

        /// <summary>
        /// 新增企业
        /// </summary>
        /// <param name="dto">企业信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> AddFirm(FirmListDto dto)
        {
            return await _firmService.AddFirm(dto);
        }

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="dto">企业信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> UptFirm(FirmListDto dto)
        {
            return await _firmService.UptFirm(dto);
        }

        /// <summary>
        /// 删除企业（逻辑删除）
        /// </summary>
        /// <param name="id">企业Id</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> DeleteFirm(string id)
        {
            return await _firmService.DeleteFirm(id);
        }

        /// <summary>
        /// 获取企业类型枚举列表
        /// </summary>
        /// <returns>企业类型列表</returns>
        [HttpGet]
        [AllowAnonymous]
        public ResponseReturnsEntity<List<object>> GetFirmTypeList()
        {
            try
            {
                var firmTypes = Enum.GetValues(typeof(FirmTypeEnum))
                    .Cast<FirmTypeEnum>()
                    .Select(ft => new
                    {
                        Value = (int)ft,
                        Name = ft.ToString(),
                        //Description = GetEnumDescription(ft)
                    })
                    .ToList<object>();

                return ResponseReturnsEntity<List<object>>.Success(firmTypes);
            }
            catch (Exception ex)
            {
                return ResponseReturnsEntity<List<object>>.Fail($"获取企业类型列表失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="enumValue">枚举值</param>
        /// <returns>描述文本</returns>
        private string GetEnumDescription(Enum enumValue)
        {
            var field = enumValue.GetType().GetField(enumValue.ToString());
            var attribute = field?.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? enumValue.ToString();
        }
    }
}